package com.wjqy.service.impl;

import com.wjqy.entity.Cdkey;
import com.wjqy.entity.Comment;
import com.wjqy.entity.User;
import com.wjqy.mapper.UserMapper;
import com.wjqy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> findAll() {
        List<User> rs = userMapper.findAll();
        return rs;
    }

    public int insertComment(Comment comment) {
        return userMapper.insertComment(comment);
    }

    public boolean useCDK(String cdk, Integer userId) {
        int status = userMapper.findCDK(cdk);
        if (status == 1) {
            Cdkey cdkey = new Cdkey();
            cdkey.setUseTime(new Date());
            cdkey.setUserId(userId);
            cdkey.setCdk(cdk);
            cdkey.setStatus((byte) 2);
            int i = userMapper.useCDK(cdkey);
            return i > 0;
        }
        return false;
    }

    public boolean bindSuperior(User user, String code) {
        User superior = userMapper.getUser(code);
        user.setLevel((byte) (superior.getLevel() + 1));
        user.setScenicId(superior.getScenicId());
        user.setSuperior(superior.getId());
        int i = userMapper.bindSuperior(user);
        return i > 0;
    }
}
